package com.lemontree.core.shiro.token;

import com.lemontree.core.shiro.ShiroToken;
import com.lemontree.web.entity.User;
import org.apache.shiro.SecurityUtils;

/**
 * Created by lemontree on 2017/6/26.
 */
public class TokenManager {

    /**
     * 获取当前登录用户
     * @return 当前登录用户信息
     */
    public static User getToken(){
        User token = (User) SecurityUtils.getSubject().getPrincipal();
        return token;
    }

    /***
     * 获取当前用户id
     * @return 用户id
     */
    public static Integer getUserId(){
        return getToken()==null?null:getToken().getId();
    }

    /**
     * 登录
     * @param u 登录用户 信息
     * @param RememberMe 是否记住
     * @return 当前用户信息
     */
    public static User login(User u,boolean RememberMe){
        ShiroToken token = new ShiroToken(u.getUserName(),u.getPassword());
        token.setRememberMe(RememberMe);
        SecurityUtils.getSubject().login(token);
        return getToken();
    }

    public static void logout(){
        SecurityUtils.getSubject().logout();
    }
}
